package com.itcast.donkey.adapters;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itcast.donkey.R;
import com.itcast.donkey.activity.Detailed_Travel_area_Activity;
import com.itcast.donkey.modles.DetailedTopicModles;
import com.itcast.donkey.span.DetailedTopicClickSpan;
import com.squareup.picasso.Picasso;

/**
 * Created by lanouhn on 16/8/27.
 */
public class DetailedTopicAdapter extends RecyclerView.Adapter<DetailedTopicAdapter.DetailedViewHolder> {
    private Context context;
    private DetailedTopicModles modles;
    private int count;
    SpannableString spanttt;
    DetailedTopicClickSpan clickttt;

    public DetailedTopicAdapter(Context context, DetailedTopicModles modlesArrayList) {
        this.context = context;
        this.modles = modlesArrayList;

        this.count = modlesArrayList.getArticle_sections().size() + 1;

    }

    @Override
    public DetailedViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_detalied_topic, null);
        DetailedViewHolder holder = new DetailedViewHolder(view);
        return holder;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public void onBindViewHolder(DetailedViewHolder holder, final int position) {
        if (position == 0) {
            holder.tvHydrogenName.setText(modles.getName());
            holder.tvHydrogenTitle.setText(modles.getTitle());
            holder.tvHydrogenName.setVisibility(View.VISIBLE);
            holder.tvHydrogenTitle.setVisibility(View.VISIBLE);
            holder.ivHydrogenBackground.setVisibility(View.VISIBLE);
            holder.ivHydrogenLine.setVisibility(View.VISIBLE);
            Picasso.with(context).load(modles.getImage_url()).into(holder.ivHydrogenBackground);

        } else {
            final int i = position - 1;
            if (!"".equals(modles.getArticle_sections().get(i).getTitle())) {
                holder.tvTitle.setText(modles.getArticle_sections().get(i).getTitle());
                holder.tvTitle.setVisibility(View.VISIBLE);
            }
            if (!"".equals(modles.getArticle_sections().get(i).getImage_url())) {
                Picasso.with(context).load(modles.getArticle_sections().get(i).getImage_url()).into(holder.ivHydrogenBackground);
                holder.ivHydrogenBackground.setVisibility(View.VISIBLE);
            }
            if (!"".equals(modles.getArticle_sections().get(i).getDescription())) {

                String str = modles.getArticle_sections().get(i).getDescription();
                String[] strs = str.split(" ");
                for (int j = 0; j < strs.length; j++) {
                    if (strs[j].contains("@")) {
                        String[] ids = strs[j].split("@");
                        String id = ids[1];
                        //获取对应id的名称
                        String name = "蝉小队";
                        ids[1] = "@" + name;
                        spanttt = new SpannableString(ids[1]);
                        clickttt = new DetailedTopicClickSpan(ids[1], context);
                        spanttt.setSpan(clickttt, 0, ids[1].length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                        ids[1] = "";
                        for (int z = 1; z < ids.length; z++) {
                            strs[j] = ids[0];
                            strs[j] += ids[z];
                        }
                    } else {
                        holder.tvTitleDescription.setText(modles.getArticle_sections().get(i).getDescription());
                    }
                }
                for (int z = 1; z < strs.length; z++) {
                    if (z == 1) {
                        holder.tvTitleDescription.setText(strs[0]);
                    }
                    if (null != spanttt) {
                        holder.tvTitleDescription.append(spanttt);
                        holder.tvTitleDescription.setMovementMethod(LinkMovementMethod.getInstance());
                    }
                    holder.tvTitleDescription.append(strs[z]);

                }
                holder.tvTitleDescription.setVisibility(View.VISIBLE);
                holder.viewDetailedLine.setVisibility(View.VISIBLE);
            }
            /**
             * 实体类有问题，DetailedTopicModles
             */
            if (modles.getArticle_sections().get(i).getNote() != null) {
                holder.tvDetailedName.setText("@" + modles.getArticle_sections().get(i).getNote().getUser_name());
                holder.tvDetailedName.setVisibility(View.VISIBLE);
                holder.tvDetailedTrip.setText(modles.getArticle_sections().get(i).getNote().getTrip_name());
                holder.tvDetailedTrip.setVisibility(View.VISIBLE);
            }
            if (modles.getArticle_sections().get(i).getAttraction() != null) {
                holder.lin_click.setVisibility(View.VISIBLE);
                holder.tvDetailedAttractionName.setText(modles.getArticle_sections().get(i).getAttraction().getName());
                holder.lin_click.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, Detailed_Travel_area_Activity.class);
                        intent.putExtra("id", modles.getArticle_sections().get(i).getAttraction().getId());
                        context.startActivity(intent);
                    }
                });

            }

        }

    }

    @Override
    public int getItemCount() {
        return count;
    }

    public class DetailedViewHolder extends RecyclerView.ViewHolder {
        private TextView tvTitle;
        private TextView tvDescription;
        private RelativeLayout rel;
        private ImageView ivHydrogenBackground;
        private TextView tvHydrogenName;
        private ImageView ivHydrogenLine;
        private TextView tvHydrogenTitle;
        private TextView tvTitleDescription;
        private ImageView imageUrl;
        private View viewDetailedLine;

        private TextView tvDetailedTrip;
        private TextView tvDetailedName;

        private TextView tvDetailedAttractionName;

        private LinearLayout lin_click;

        public DetailedViewHolder(View itemView) {
            super(itemView);
            tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
            tvDescription = (TextView) itemView.findViewById(R.id.tv_description);
            rel = (RelativeLayout) itemView.findViewById(R.id.rel);
            ivHydrogenBackground = (ImageView) itemView.findViewById(R.id.iv_hydrogen_background);
            tvHydrogenName = (TextView) itemView.findViewById(R.id.tv_hydrogen_name);
            ivHydrogenLine = (ImageView) itemView.findViewById(R.id.iv_hydrogen_line);
            tvHydrogenTitle = (TextView) itemView.findViewById(R.id.tv_hydrogen_title);
            tvTitleDescription = (TextView) itemView.findViewById(R.id.tv_title_description);
            imageUrl = (ImageView) itemView.findViewById(R.id.image_url);
            viewDetailedLine = (View) itemView.findViewById(R.id.view_detailed_line);

            tvDetailedTrip = (TextView) itemView.findViewById(R.id.tv_detailed_trip);
            tvDetailedName = (TextView) itemView.findViewById(R.id.tv_detailed_name);

            tvDetailedAttractionName = (TextView) itemView.findViewById(R.id.tv_detailed_attraction_name);

            lin_click = (LinearLayout) itemView.findViewById(R.id.lin_click_item_detailed_topic);
        }
    }
}
